﻿  var full_address_obj = {
    country: jQuery('#Profile_country'),
    city: jQuery('#Profile_city'),
    address: jQuery('#Profile_address')
  };
  
  var geo_obj = {
    geocoder: new google.maps.Geocoder(),
    latlng_input: jQuery('#Profile_latlng'),
    gmarker: null
  };
  
  var map_options = {
    center: new google.maps.LatLng(52.0688122, 19.4797444),
    zoom: 6,                                                    // zoom Poland
    mapTypeId: google.maps.MapTypeId.ROADMAP
  }
  
  var my_map = {
    google_map: null,
    map_elem: jQuery('.map_section')
  };
  
  function init_map(html_elem) {
    my_map.google_map = new google.maps.Map(html_elem, map_options);
  };
  
  function is_valid_full_address_obj() {
    if(full_address_obj.country && full_address_obj.city && full_address_obj.address) {
      return true;
    }
    
    return false;
  }
  
  function get_full_address_value() {
    if(is_valid_full_address_obj()) {
      if(full_address_obj.country.val() && full_address_obj.city.val() && full_address_obj.address.val()) {
        return full_address_obj.country.val() + ', ' + full_address_obj.city.val() + ', ' + full_address_obj.address.val();
      }
    }
    
    return null;
  }
  
  
  function get_converted_to_latlng(str) {
    var latlng = str.split(',');
    return new google.maps.LatLng(latlng[0], latlng[1]);
  }
  
  function geocode_by_proper_obj() {
    if(geo_obj.latlng_input.val()) {
      //console.log('next set!');
      set_by_new_gmarker(get_converted_to_latlng(geo_obj.latlng_input.val()), get_full_address_value());
    } else {
      //console.log('first init!');
      geocode_full_address();
    }
  }
  
  function geocode_full_address() {
    var full_address = get_full_address_value();
    if(full_address) {
      geo_obj.geocoder.geocode({address: full_address}, process_geocode);
      //console.log('zmiana: ' + full_address);
    }
  }
  
  function init_change_geocoder_by_events() {
    if(is_valid_full_address_obj()) {
      jQuery.each(full_address_obj, function(name, value) {
        value.change(function() {
          geocode_full_address();
        });
      });
    }
  };
  
  function set_by_new_gmarker(location, formatted_address) {
    //console.log(formatted_address);
    if (geo_obj.gmarker) {
      geo_obj.gmarker.setMap(null);
    }
    //console.log('location:' + location);
    geo_obj.gmarker = new google.maps.Marker({
      map       : my_map.google_map,
      position  : location,
      draggable : true
    });
    
    my_map.google_map.setCenter(location);
    my_map.google_map.setZoom(17);
    
    geo_obj.latlng_input.val(geo_obj.gmarker.position.toString().slice(1, -1));
    
    var info_window = new google.maps.InfoWindow();
    info_window.setContent(formatted_address);
    
    google.maps.event.addListener(geo_obj.gmarker, 'click', function() {
      info_window.open(my_map.google_map, geo_obj.gmarker);
    });
    google.maps.event.addListener(geo_obj.gmarker, 'dragend', function() {
      geo_obj.latlng_input.val(geo_obj.gmarker.position.toString().slice(1, -1));
      my_map.google_map.setCenter(geo_obj.gmarker.position);
    });
  }
  
  function process_geocode(results, status) {
    if(status == google.maps.GeocoderStatus.OK) {
      set_by_new_gmarker(results[0].geometry.location, results[0].formatted_address);//.geometry.location);
    }
  }
  
  /* Workaround do pól formularza */
  
  function handle_map_status_change(checkbox_elem, elem_to_change) {
    if(jQuery(checkbox_elem).is(':checked')) {
      elem_to_change.val('1');
      my_map.map_elem.slideDown();
    } else {
      elem_to_change.val('0');
      my_map.map_elem.slideUp();
    }
  }
  
  var agent_on_map = jQuery('#Profile_agent_on_map');
  var show_me_on_map = jQuery('#Profile_show_me_on_map');
  if(agent_on_map.val() == '1') {
    show_me_on_map.attr('checked', true);
  }
  
  show_me_on_map.change(function(e) {
    handle_map_status_change(e.currentTarget, agent_on_map)
  });
  
  /* Koniec Workaround'u do pól formularza */
  
  function initialize_map(map_html_elem) {    
    init_map(map_html_elem);
    init_change_geocoder_by_events();
    google.maps.event.addListenerOnce(my_map.google_map, 'idle', function() {   // events order: center_changed, zoom_changed, bounds_changed, idle, tilesloaded (http://stackoverflow.com/questions/1544387/google-maps-api-v3-earliest-event-to-inform-me-of-the-bounds)
      handle_map_status_change(show_me_on_map, agent_on_map);
    });

    geocode_by_proper_obj();
  }